using System;
using System.Windows.Forms;

namespace Front;

public partial class MainForm : Form
{
    // 登录用户名（用于显示当前登录人）
    private readonly string _loginUsername;

    public MainForm(string loginUsername)
    {
        _loginUsername = loginUsername;
        InitializeComponent();
        InitMainControls();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        this.Text = $"图书管理系统 - 主界面（当前用户：{_loginUsername}）";
    }

    // 初始化主界面控件（功能菜单按钮）
    private void InitMainControls()
    {
        // 图书管理按钮
        Button btnBookManage = new Button
        {
            Text = "图书管理",
            Location = new System.Drawing.Point(80, 60),
            Size = new System.Drawing.Size(120, 40),
            Font = new System.Drawing.Font("微软雅黑", 10)
        };
        btnBookManage.Click += (s, e) => OpenChildForm(new BookManagementForm());
        this.Controls.Add(btnBookManage);

        // 读者管理按钮
        Button btnReaderManage = new Button
        {
            Text = "读者管理",
            Location = new System.Drawing.Point(250, 60),
            Size = new System.Drawing.Size(120, 40),
            Font = new System.Drawing.Font("微软雅黑", 10)
        };
        btnReaderManage.Click += (s, e) => OpenChildForm(new ReaderManagementForm());
        this.Controls.Add(btnReaderManage);

        // 借阅管理按钮
        Button btnBorrowManage = new Button
        {
            Text = "借阅管理",
            Location = new System.Drawing.Point(420, 60),
            Size = new System.Drawing.Size(120, 40),
            Font = new System.Drawing.Font("微软雅黑", 10)
        };
        btnBorrowManage.Click += (s, e) => OpenChildForm(new BorrowManagementForm());
        this.Controls.Add(btnBorrowManage);

        // 数据统计按钮
        Button btnStatistics = new Button
        {
            Text = "数据统计",
            Location = new System.Drawing.Point(80, 150),
            Size = new System.Drawing.Size(120, 40),
            Font = new System.Drawing.Font("微软雅黑", 10)
        };
        btnStatistics.Click += (s, e) => OpenChildForm(new StatisticsForm());
        this.Controls.Add(btnStatistics);

        // 系统设置按钮
        Button btnSystem = new Button
        {
            Text = "系统设置",
            Location = new System.Drawing.Point(250, 150),
            Size = new System.Drawing.Size(120, 40),
            Font = new System.Drawing.Font("微软雅黑", 10)
        };
        btnSystem.Click += (s, e) => MessageBox.Show("系统设置功能待实现！", "提示");
        this.Controls.Add(btnSystem);

        // 退出登录按钮
        Button btnLogout = new Button
        {
            Text = "退出登录",
            Location = new System.Drawing.Point(420, 150),
            Size = new System.Drawing.Size(120, 40),
            Font = new System.Drawing.Font("微软雅黑", 10),
            ForeColor = System.Drawing.Color.Red
        };
        btnLogout.Click += (s, e) => Logout();
        this.Controls.Add(btnLogout);

        // 主窗体设置
        this.Size = new System.Drawing.Size(650, 300);
        this.StartPosition = FormStartPosition.CenterScreen;
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.FormClosing += MainForm_FormClosing; // 窗体关闭事件
    }

    /// <summary>
    /// 打开子窗体（关闭其他子窗体，只显示当前一个）
    /// </summary>
    private void OpenChildForm(Form childForm)
    {
        // 关闭所有已打开的子窗体
        foreach (Form form in Application.OpenForms)
        {
            if (form != this)
            {
                form.Close();
            }
        }
        childForm.Show(); // 显示当前子窗体
    }

    /// <summary>
    /// 退出登录（返回登录界面）
    /// </summary>
    private void Logout()
    {
        DialogResult result = MessageBox.Show("确定要退出登录吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
        {
            Form1 loginForm = new Form1();
            loginForm.Show();
            this.Close();
        }
    }

    /// <summary>
    /// 主窗体关闭时退出应用
    /// </summary>
    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        Application.Exit();
    }
}

// 以下为子窗体空实现（后续可补全功能）
namespace Front
{
    // 图书管理子窗体
    public partial class BookManagementForm : Form
    {
        public BookManagementForm()
        {
            InitializeComponent();
            this.Text = "图书管理";
            this.Size = new System.Drawing.Size(800, 500);
            this.StartPosition = FormStartPosition.CenterScreen;
        }
    }

    // 读者管理子窗体
    public partial class ReaderManagementForm : Form
    {
        public ReaderManagementForm()
        {
            InitializeComponent();
            this.Text = "读者管理";
            this.Size = new System.Drawing.Size(800, 500);
            this.StartPosition = FormStartPosition.CenterScreen;
        }
    }

    // 借阅管理子窗体
    public partial class BorrowManagementForm : Form
    {
        public BorrowManagementForm()
        {
            InitializeComponent();
            this.Text = "借阅管理";
            this.Size = new System.Drawing.Size(800, 500);
            this.StartPosition = FormStartPosition.CenterScreen;
        }
    }

    // 数据统计子窗体
    public partial class StatisticsForm : Form
    {
        public StatisticsForm()
        {
            InitializeComponent();
            this.Text = "数据统计";
            this.Size = new System.Drawing.Size(800, 500);
            this.StartPosition = FormStartPosition.CenterScreen;
        }
    }
}